<?php

/*
 * This file is part of the FOSElasticaBundle package.
 *
 * (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace FOS\ElasticaBundle\Configuration;

/**
 * Index template configuration class.
 *
 * @author Dmitry Balabka <dmitry.balabka@intexsys.lv>
 *
 * @phpstan-import-type TConfig from IndexConfigInterface
 */
class IndexTemplateConfig implements IndexConfigInterface
{
    use IndexConfigTrait;

    /**
     * Index name pattern.
     *
     * @var list<non-empty-string>
     */
    private array $indexPatterns;

    /**
     * Constructor expects an array as generated by the Container Configuration builder.
     *
     * @phpstan-param TConfig $config
     */
    public function __construct(array $config)
    {
        $this->elasticSearchName = $config['elasticsearch_name'] ?? $config['name'];
        $this->name = $config['name'];
        $this->settings = $config['settings'] ?? [];
        $this->config = $config['config'];
        $this->mapping = $config['mapping'];

        if (!isset($config['index_patterns']) || !\is_array($config['index_patterns']) || \count($config['index_patterns']) < 1) {
            throw new \InvalidArgumentException('Index patterns value(s) must be set');
        }

        $this->indexPatterns = $config['index_patterns'];
    }

    /**
     * Gets index name pattern.
     *
     * @return list<non-empty-string>
     */
    public function getIndexPatterns(): array
    {
        return $this->indexPatterns;
    }
}
